// Copyright (C) 2021-2022 Intel Corporation
// Copyright (C) 2022-2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import React, { useCallback } from 'react';
import Text from 'antd/lib/typography/Text';
import Collapse from 'antd/lib/collapse';

import ObjectButtonsContainer from 'containers/annotation-page/standard-workspace/objects-side-bar/object-buttons';
import ItemDetailsContainer from 'containers/annotation-page/standard-workspace/objects-side-bar/object-item-details';
import { ObjectType, ShapeType, ColorBy } from 'reducers';
import classNames from 'classnames';
import { AnnotationStatus, JobStatusEnum } from 'typings/annotion/task';
import useAnnotation from 'hooks/useAnnotation';
import { useParams } from 'react-router-dom-v5-compat';
import FirstReviewItem from './review/first/first-review-item';
import SecondReviewItem from './review/second/second-review-item';
import ObjectItemElementComponent from './object-item-element';
import ItemBasics from './object-item-basics';
import AnnotationDetail from './detail/annotation-detail';
import FirstReviewDetail from './detail/first-review-detail';
import SecondReviewDetail from './detail/second-review-detail';

interface Props {
    normalizedKeyMap: Record<string, string>;
    readonly: boolean;
    activated: boolean;
    objectType: ObjectType;
    shapeType: ShapeType;
    clientID: number;
    serverID: number | null;
    labelID: number;
    isGroundTruth: boolean;
    locked: boolean;
    elements: number[];
    color: string;
    colorBy: ColorBy;
    labels: any[];
    attributes: any[];
    jobInstance: any;
    activate(activeElementID?: number): void;
    copy(): void;
    propagate(): void;
    createURL(): void;
    switchOrientation(): void;
    toBackground(): void;
    toForeground(): void;
    remove(): void;
    changeLabel(label: any): void;
    changeColor(color: string): void;
    resetCuboidPerspective(): void;
    edit(): void;
    slice(): void;
}

function ObjectItemComponent(props: Props): JSX.Element {
    const {
        activated,
        readonly,
        objectType,
        shapeType,
        clientID,
        serverID,
        locked,
        labelID,
        color,
        colorBy,
        elements,
        attributes,
        labels,
        normalizedKeyMap,
        isGroundTruth,
        activate,
        copy,
        propagate,
        createURL,
        switchOrientation,
        toBackground,
        toForeground,
        remove,
        changeLabel,
        changeColor,
        resetCuboidPerspective,
        edit,
        slice,
        jobInstance,
    } = props;

    const { jobStatus } = useAnnotation();
    const params = useParams();

    const type =
        objectType === ObjectType.TAG
            ? ObjectType.TAG.toUpperCase()
            : `${shapeType.toUpperCase()} ${objectType.toUpperCase()}`;

    const className = classNames({
        'cvat-objects-sidebar-state-item': true,
        'cvat-objects-sidebar-state-active-item': activated,
        'cvat-objects-sidebar-state-item-first-preview': true,
    });

    const activateState = useCallback(() => {
        activate();
    }, []);

    const collapseLabel = (
        <ItemBasics
            jobInstance={jobInstance}
            readonly={readonly}
            serverID={serverID}
            clientID={clientID}
            labelID={labelID}
            labels={labels}
            shapeType={shapeType}
            objectType={objectType}
            color={color}
            colorBy={colorBy}
            type={type}
            locked={locked}
            isGroundTruth={isGroundTruth}
            copyShortcut={normalizedKeyMap.COPY_SHAPE}
            pasteShortcut={normalizedKeyMap.PASTE_SHAPE}
            propagateShortcut={normalizedKeyMap.PROPAGATE_OBJECT}
            toBackgroundShortcut={normalizedKeyMap.TO_BACKGROUND}
            toForegroundShortcut={normalizedKeyMap.TO_FOREGROUND}
            removeShortcut={normalizedKeyMap.DELETE_OBJECT}
            changeColorShortcut={normalizedKeyMap.CHANGE_OBJECT_COLOR}
            sliceShortcut={normalizedKeyMap.SWITCH_SLICE_MODE}
            changeLabel={changeLabel}
            changeColor={changeColor}
            copy={copy}
            remove={remove}
            propagate={propagate}
            createURL={createURL}
            switchOrientation={switchOrientation}
            toBackground={toBackground}
            toForeground={toForeground}
            resetCuboidPerspective={resetCuboidPerspective}
            edit={edit}
            slice={slice}
        />
    );

    return (
        <div className='flex mb-1px'>
            <div onMouseEnter={activateState} id={`cvat-objects-sidebar-state-item-${clientID}`} className={className}>
                {/* 标注 */}
                {/* 标注员/初审驳回 */}
                {!!attributes.length &&
                    params.action === 'annotation' &&
                    [JobStatusEnum.WAITING_ANNOTATION, JobStatusEnum.FIRST_REVIEW_REJECTED].includes(jobStatus) && (
                        <ItemDetailsContainer
                            readonly={readonly}
                            clientID={clientID}
                            parentID={null}
                            collapseLabel={collapseLabel}
                        />
                    )}
                {/* 审核 */}
                {params.action === 'review' && (
                    <>
                        {/* 初审/复审驳回 */}
                        {!!attributes.length &&
                            [JobStatusEnum.SECOND_REVIEW_REJECTED, JobStatusEnum.WAITING_FIRST_REVIEW].includes(
                                jobStatus,
                            ) && (
                                <FirstReviewItem
                                    readonly={readonly}
                                    clientID={clientID}
                                    parentID={null}
                                    collapseLabel={collapseLabel}
                                />
                            )}
                        {/* 复审 */}
                        {!!attributes.length && [JobStatusEnum.WAITING_SECOND_REVIEW].includes(jobStatus) && (
                            <SecondReviewItem
                                readonly={readonly}
                                clientID={clientID}
                                parentID={null}
                                collapseLabel={collapseLabel}
                            />
                        )}
                    </>
                )}

                {/* 详情 */}
                {params.action === 'view' && (
                    <>
                        {/* 待标注/初审驳回 */}
                        {[
                            JobStatusEnum.WAITING_ANNOTATION,
                            JobStatusEnum.FIRST_REVIEW_REJECTED,
                            JobStatusEnum.CLOSED,
                        ].includes(jobStatus) && <AnnotationDetail readonly={readonly} clientID={clientID} />}
                        {/* 待初审/复审驳回 */}
                        {[JobStatusEnum.SECOND_REVIEW_REJECTED, JobStatusEnum.WAITING_FIRST_REVIEW].includes(
                            jobStatus,
                        ) && <FirstReviewDetail readonly={readonly} clientID={clientID} />}
                        {/* d待复审/复审通过 */}
                        {[JobStatusEnum.WAITING_SECOND_REVIEW, JobStatusEnum.SECOND_REVIEW_APPROVED].includes(
                            jobStatus,
                        ) && <SecondReviewDetail readonly={readonly} clientID={clientID} />}
                    </>
                )}
                {/* action */}
                <div className='mt-3 px-3 pb-3'>
                    <ObjectButtonsContainer readonly={readonly} clientID={clientID} />
                </div>
            </div>
        </div>
    );
}

export default React.memo(ObjectItemComponent);
